package org.exhaust.framework.net.test.protocol.client.tcp.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.exhaust.framework.net.common.protocol.decoder.ByteBufDecoderHelper;
import org.exhaust.framework.net.test.protocol.client.tcp.server.LoginResponse;
import org.exhaust.framework.net.test.protocol.client.tcp.server.ServerGprsMsg;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class ServerGprsMsgDecoderHelper implements ByteBufDecoderHelper<ServerGprsMsg> {

    private static final Map<Integer, Class<? extends ServerGprsMsg>> clazzMap = new ConcurrentHashMap<>();

    static {
        clazzMap.put(1, LoginResponse.class);
    }

    @Override
    public ServerGprsMsg decode(ChannelHandlerContext ctx, ByteBuf in) {
        log.info("receive from server:" + ByteBufUtil.hexDump(in));
        int offset = in.readerIndex();
        final int msgType = in.getUnsignedByte(offset);
        if (msgType == 1) {
            LoginResponse response = new LoginResponse();
            response.decode(in, null);
            return response;
        }
        return null;
    }
}
